package cn.com.jd.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.com.jd.common.CommonConstant;
import cn.com.jd.dao.GameaccountDtoMapper;
import cn.com.jd.dao.UserInfoDtoMapper;
import cn.com.jd.dto.AccountManageDto;
import cn.com.jd.dto.StatusDto;
import cn.com.jd.model.GameaccountDto;
import cn.com.jd.model.GameaccountDtoKey;
import cn.com.jd.model.UserInfoDto;
import cn.com.jd.service.AccountManageService;

@Service("AccountManageService")
public class AccountManageServiceImpl implements AccountManageService  {

	@Resource
	private UserInfoDtoMapper userInfoDtoMapper;
	@Resource
	private GameaccountDtoMapper gameaccountDtoMapper;
	
	@Override
	public StatusDto addAccount(AccountManageDto am){
		StatusDto sts = new StatusDto();
		sts.setSts(CommonConstant.NORMAL);
		GameaccountDtoKey gameaccountDtoKey = new GameaccountDtoKey();
		gameaccountDtoKey.setUserId(am.getUserId());
		gameaccountDtoKey.setAccountId(am.getGameId());
		GameaccountDto gc = gameaccountDtoMapper.selectByPrimaryKey(gameaccountDtoKey);
		if(gc != null){
			sts.setSts(CommonConstant.ERROR);
			sts.setMsg(CommonConstant.DUPLICATE_GAME_ID);
			return sts;
		} else{
			int cnt = gameaccountDtoMapper.countGameAccount(gameaccountDtoKey);
			if(1 < cnt){
				sts.setSts(CommonConstant.ERROR);
				sts.setMsg(CommonConstant.BY_TWO_USERS_BINDED);
				return sts;
			} else {
				GameaccountDto ga = new GameaccountDto();
				ga.setUserId(am.getUserId());
				ga.setAccountId(am.getGameId());
//				ga.setAccountType(am.getAccountType());
				gameaccountDtoMapper.insert(ga);
/*				if(checkMaxAccelerateCount(am.getUserId())){
					sts.setMaxAccelerateFlag(CommonConstant.MAX_ACCELERATE);
				}*/
			}
		}
		return sts;
	}
	
	@Override
	public StatusDto deleteAccount(AccountManageDto am){
		StatusDto sts = new StatusDto();
		sts.setSts(CommonConstant.NORMAL);
		GameaccountDtoKey gameaccountDtoKey = new GameaccountDtoKey();
		gameaccountDtoKey.setUserId(am.getUserId());
		gameaccountDtoKey.setAccountId(am.getGameId());
		gameaccountDtoMapper.deleteByPrimaryKey(gameaccountDtoKey);
		if(checkMaxAccelerateCount(am.getUserId())){
			sts.setMaxAccelerateFlag(CommonConstant.MAX_ACCELERATE);
		}
		return sts;
	}
	
	@Override
	public StatusDto updateAccount(AccountManageDto am){
		StatusDto sts = new StatusDto();
		sts.setSts(CommonConstant.NORMAL);
		GameaccountDto ga = new GameaccountDto();
		ga.setUserId(am.getUserId());
		ga.setAccountId(am.getGameId());
//		ga.setAccountType(am.getAccountType());
		gameaccountDtoMapper.updateByPrimaryKeySelective(ga);
		return sts;
	}
	
	@Override
	public StatusDto accelerateAccount(AccountManageDto am){
		StatusDto sts = new StatusDto();
		sts.setSts(CommonConstant.NORMAL);
		if(checkMaxAccelerateCount(am.getUserId())){
			sts.setSts(CommonConstant.ERROR);
			sts.setMaxAccelerateFlag(CommonConstant.MAX_ACCELERATE);
			sts.setMsg(CommonConstant.MAX_ACCELERATE_COUNT_REACH);
			return sts;
		} else {
			UserInfoDto ui = userInfoDtoMapper.selectByPrimaryKey(am.getUserId());
			GameaccountDto ga = new GameaccountDto();
			ga.setUserId(am.getUserId());
			ga.setAccountId(am.getGameId());
			ga.setExpireDate(ui.getExpireDate());
			ga.setAccelerateFlag(CommonConstant.ACCELERATE);
			gameaccountDtoMapper.accelerateAccount(ga);
		}
		return sts;
	}
	
	@Override
	public StatusDto deaccelerateAccount(AccountManageDto am){
		StatusDto sts = new StatusDto();
		sts.setSts(CommonConstant.NORMAL);
		GameaccountDto ga = new GameaccountDto();
		ga.setUserId(am.getUserId());
		ga.setAccountId(am.getGameId());
		ga.setAccelerateFlag(CommonConstant.UNACCELERATE);
		gameaccountDtoMapper.accelerateAccount(ga);
		return sts;
	}
	
	@Override
	public List<GameaccountDto> getAccountList(String userId){
		List<GameaccountDto> al = gameaccountDtoMapper.selectByUserid(userId);
		return al;
	}
	
	private boolean checkMaxAccelerateCount(String userid){
		UserInfoDto ui = userInfoDtoMapper.selectByPrimaryKey(userid);
		int acnt = gameaccountDtoMapper.countAccelerateAccount(userid);
		if(acnt >= ui.getMaxAccount()){
			return true;
		} else {
			return false;
		}
	}
}
